package com.lrkj.payfee.service.impl;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.core.domain.entity.OrgTree;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.payfee.domain.ChargeNorm;
import com.lrkj.payfee.mapper.ChargeNormMapper;
import com.lrkj.payfee.service.IChargeNormService;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收费标准Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-07-26
 */
@Service
public class ChargeNormServiceImpl implements IChargeNormService {
    @Autowired
    private ChargeNormMapper chargeNormMapper;

    @Autowired
    private IOrgTreeService orgTreeService;

    /**
     * 查询收费标准
     *
     * @param normId 收费标准ID
     * @return 收费标准
     */
    @Override
    public ChargeNorm selectChargeNormById(String normId) {
        return chargeNormMapper.selectChargeNormById(normId);
    }

    /**
     * 查询收费标准列表
     *
     * @param chargeNorm 收费标准
     * @return 收费标准
     */
    @Override
    public List<ChargeNorm> selectChargeNormList(ChargeNorm chargeNorm) {
        return chargeNormMapper.selectChargeNormList(chargeNorm);
    }

    /**
     * 新增收费标准
     *
     * @param chargeNorm 收费标准
     * @return 结果
     */
    @Override
    public int insertChargeNorm(ChargeNorm chargeNorm) {
        chargeNorm.setNormId(IdUtils.simpleUUID());
        chargeNorm.setCreateBy(SecurityUtils.getUsername());
        chargeNorm.setCreateTime(DateUtils.getNowDate());
        this.setUnitInfo(chargeNorm);
        return chargeNormMapper.insertChargeNorm(chargeNorm);
    }

    /**
     * 修改收费标准
     *
     * @param chargeNorm 收费标准
     * @return 结果
     */
    @Override
    public int updateChargeNorm(ChargeNorm chargeNorm) {
        this.setUnitInfo(chargeNorm);
        chargeNorm.setUpdateBy(SecurityUtils.getUsername());
        chargeNorm.setUpdateTime(DateUtils.getNowDate());
        return chargeNormMapper.updateChargeNorm(chargeNorm);
    }

    /**
     * 批量删除收费标准
     *
     * @param normIds 需要删除的收费标准ID
     * @return 结果
     */
    @Override
    public int deleteChargeNormByIds(String[] normIds) {
        orgTreeService.deleteOrgTreeByIds(normIds);
        return chargeNormMapper.deleteChargeNormByIds(normIds);
    }

    /**
     * 删除收费标准信息
     *
     * @param normId 收费标准ID
     * @return 结果
     */
    @Override
    public int deleteChargeNormById(String normId) {
        orgTreeService.deleteOrgTreeById(normId);
        return chargeNormMapper.deleteChargeNormById(normId);
    }


    private void setUnitInfo(ChargeNorm bean) {
        OrgTree parentBean = orgTreeService.selectOrgTreeById(bean.getDeptId());
        bean.setAncestors(parentBean.getAncestors() + "," + bean.getDeptId());
        orgTreeService.insertOrgTree(bean.getNormId(), bean.getNormNames(), bean.getDeptId(), OrgConstants.norm, bean.getAncestors());
    }

    //获取收费标准Map
    @Override
    public Map<String, ChargeNorm> getChargeNormMap(String commintyId) {
        Map<String, ChargeNorm> map = new HashMap<>();
        ChargeNorm eg = new ChargeNorm();
        eg.setDeptId(commintyId);//收费标准绑定在小区下
        List<ChargeNorm> chargeNorms = chargeNormMapper.selectChargeNormList(eg);
        chargeNorms.stream().forEach(x -> {
            map.put(x.getNormId(), x);
        });
        return map;
    }

    @Override
    public ChargeNorm selectChargeById(String chargeId) {
        return chargeNormMapper.selectChargeById(chargeId);
    }

    @Override
    public List<ChargeNorm> selectChargeNormIN(ChargeNorm chargeNorm) {
        return chargeNormMapper.selectChargeNormIN(chargeNorm);
    }
}
